//
//  SaturdayViewController.m
//  星期6
//
//  Created by 宗镇荣 on 16/2/22.
//  Copyright © 2016年 zzr. All rights reserved.
//

#import "SaturdayViewController.h"
#import "TabView.h"

@interface SaturdayViewController ()<UITableViewDataSource,UITableViewDelegate, ImagesScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>{

    ImagesScrollView *_imagesScrollView;
}
@property (nonatomic) Reachability *reachNet;
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *dataOne;
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) MBProgressHUD *hud;
@property (nonatomic) NSArray *banners;
@property (nonatomic, strong) TabView *tabView;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic) UITableView *tableView3;
@property (nonatomic) NSMutableArray *data3;

@property (nonatomic) UITableView *tableView4;
@property (nonatomic) NSMutableArray *data4;

@property (nonatomic) UICollectionView *collectionView;

@property (nonatomic) NSMutableArray *dataSource;

@end

@implementation SaturdayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self refreshData];
    [self createScrollView];
}

- (void)refreshData {

    _currentPage = 1;
    [self downloadData];
}

- (void)loadMore {

    _currentPage++;
    [self downloadData];
}

- (void)createScrollView {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:@"http://api.guozhoumoapp.com/v1/banners?channel=iOS" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *data = rootDict[@"data"];
       
        //        NSArray *bannarsArray = dataDict[@"banners"];
        //
        //        NSArray *models = [HeadSModel arrayOfModelsFromDictionaries:bannarsArray];
        //        NSLog(@"%@", models);
        
        self.banners = data[@"banners"];
        [_imagesScrollView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        QFLog(@"%@",error);
    }];
}

- (void)downloadData {

    [self.view addSubview:_hud];
    [self.hud show:YES];
    [SelectModel requestWithPageNumber:_currentPage finishBlock:^(NSMutableArray *array, NSError *error) {
       
        if (error == nil) {
            
            if (_currentPage == 1) {
                
                [self.dataSource removeAllObjects];
            }
            
            [self.dataSource addObjectsFromArray:array];
            
//            [self.tableView reloadData];
//            [_tableView.header endRefreshing];
//            [_tableView.footer endRefreshing];
            [self.collectionView reloadData];
            
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];

            
        }else {
            
            QFLog(@"%@", error);
        }
        
        [self.hud hide:YES];
    }];
    
    [SelectShopping requestWithPageNumber:_currentPage WithAppId:12 successBlock:^(NSMutableArray *array, NSError *error) {
        
        if (error == nil) {
            
            if (_currentPage == 1) {
                
                [self.dataOne removeAllObjects];
            }
            
            [self.dataOne addObjectsFromArray:array];
            
            [self.tableView reloadData];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            
        }else {
            
            QFLog(@"%@", error);
        }
        
        [self.hud hide:YES];
    }];
    
    
    
        
    
}


- (void)configUI {

    [self setNavigationTitle:@"小憩"];
//    self.tabView = [[TabView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 50)];
//    self.tabView.titleArray = @[@"精选",@"周末逛街",@"体验课",@"周边游",@"尝美食"];
//    self.tabView.delegate = self;
//    [self.view addSubview:self.tabView];
//    
//    /****下部的UIScrollView*******/
//    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64+50, kScreenWidth, kScreenHeight-64-50-49)];
//    
//    // 设置分页的属性
//    _mainScrollView.pagingEnabled = YES;
//    
//    // 设置内容视图的大小
//    _mainScrollView.contentSize = CGSizeMake(5*kScreenWidth, kScreenHeight-64-50-49);
//    _mainScrollView.showsHorizontalScrollIndicator = NO;
//    _mainScrollView.backgroundColor = [UIColor orangeColor];
//    
//    // 设置代理
//    _mainScrollView.delegate = self;
//    
//    [self.view addSubview:_mainScrollView];
//    
    
    UIViewController *oneViewController = [[UIViewController alloc] init];
    oneViewController.title = @"精选";
    UIView *collectionViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    collectionViewHeader.backgroundColor = [UIColor grayColor];
    _imagesScrollView = [[ImagesScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    _imagesScrollView.delegate = self;
    _imagesScrollView.autoScrollInterval = 3;
    _imagesScrollView.isLoop = YES;
    [collectionViewHeader addSubview:_imagesScrollView];
    [oneViewController.view addSubview:collectionViewHeader];
    oneViewController.view.backgroundColor = [UIColor whiteColor];
    oneViewController.view.tag = 11;

    UIViewController *twoViewController = [[UIViewController alloc] init];
    twoViewController.title = @"周末逛街";
    twoViewController.view.tag = 12;

    ExperienceViewController *threeViewController = [[ExperienceViewController alloc] init];
    threeViewController.title = @"体验课";
    threeViewController.view.tag = 13;
//
//    
    PlayViewController *fourViewController = [[PlayViewController alloc] init];
    fourViewController.title = @"周边游";
    fourViewController.view.tag = 14;
    fourViewController.color = [UIColor redColor];
    
    FoodViewController *fiveViewController = [[FoodViewController alloc] init];
    fiveViewController.title = @"尝美食";
    fiveViewController.view.tag = 15;
    
//    [self addChildViewController:oneViewController];
//    [self addChildViewController:twoViewController];
//    [self addChildViewController:threeViewController];
//    [self addChildViewController:fourViewController];
//    [self addChildViewController:fiveViewController];
//    
//    oneViewController.view.frame = CGRectMake(0, 0, kScreenWidth, _mainScrollView.frame.size.height);
//    twoViewController.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _mainScrollView.frame.size.height);
//    threeViewController.view.frame = CGRectMake(kScreenWidth*2, 0, kScreenWidth, _mainScrollView.frame.size.height);
//    fourViewController.view.frame = CGRectMake(kScreenWidth*3, 0, kScreenWidth, _mainScrollView.frame.size.height);
//    fiveViewController.view.frame = CGRectMake(kScreenWidth*4, 0, kScreenWidth, _mainScrollView.frame.size.height);
//    //添加到mainScrollView
//    [_mainScrollView addSubview:oneViewController.view];
//    [_mainScrollView addSubview:twoViewController.view];
//    [_mainScrollView addSubview:threeViewController.view];
//    [_mainScrollView addSubview:fourViewController.view];
//    [_mainScrollView addSubview:fiveViewController.view];
    
    SCNavTabBarController *nav  =[[SCNavTabBarController alloc] init];
    //nav.subViewControllers = @[oneViewController,twoViewController, fiveViewController, threeViewController,fourViewController];
    nav.subViewControllers = @[oneViewController,twoViewController];
    nav.showArrowButton = YES;
    [nav addParentController:self];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 -49)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [twoViewController.view addSubview:self.tableView];

    

    
    [_tableView registerClass:[SelectTableViewCell class] forCellReuseIdentifier:@"Select"];
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    _tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       
        [self loadMore];
    }];
    
    

    
   

    

    
    
    UICollectionViewFlowLayout *flowlayOut = [[UICollectionViewFlowLayout alloc]init];
    flowlayOut.sectionInset = UIEdgeInsetsMake(0, 10, 5, 10);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 170, kScreenWidth, self.view.frame.size.height - 64 - 49 ) collectionViewLayout:flowlayOut];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [oneViewController.view addSubview:_collectionView];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"SelectCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MYCELL"];
    
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self refreshData];
    }];
    _collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self loadMore];
    }];

}

- (void)creatTableViewHeader {

    UIView *collectionViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    collectionViewHeader.backgroundColor = [UIColor grayColor];
    _imagesScrollView = [[ImagesScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    _imagesScrollView.delegate = self;
    _imagesScrollView.autoScrollInterval = 3;
    _imagesScrollView.isLoop = YES;
    [collectionViewHeader addSubview:_imagesScrollView];
   // self.tableView.tableHeaderView = collectionViewHeader;
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataOne.count;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 170;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectShopping *model  = self.dataOne[indexPath.row];
    DetailTableViewController *detail = [[DetailTableViewController alloc] init];
    detail.url = model.url;
    detail.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Select" forIndexPath:indexPath];
    cell.model = self.dataOne[indexPath.row];
    return cell;
}

- (NSInteger)numberOfImagesInImagesScrollView:(ImagesScrollView *)imagesScrollView {

    return self.banners.count;
}

- (NSString *)imagesScrollView:(ImagesScrollView *)imagesScrollView imageUrlStringWithIndex:(NSInteger)index {
    
    return self.banners[index][@"image_url"];
}

#pragma  mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    SelectModel *model = self.dataSource[indexPath.row];
    DetailSatViewController *detail = [[DetailSatViewController alloc] init];
    detail.url = model.url;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:NO];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYCELL" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
#pragma  mark UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth, (kScreenWidth-50)/2);
    
}



#pragma mark - UIScrollViewDelegate

// 结束减速的时候调用 (速度为零), 不动时调用
// 代理本身传过来
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView == self.mainScrollView) {
        
        //contentOffset偏移量
        NSInteger page = scrollView.contentOffset.x/kScreenWidth;
        [self.tabView resetBtnStatus:page+100];
    }
}

#pragma mark - TabViewDelegate
//保存当前的页面
- (void)tabBtn:(NSInteger)page {
    
    // 更改偏移量
    self.mainScrollView.contentOffset = CGPointMake(page * kScreenWidth, 0);
}

#pragma mark - 懒加载
- (NSMutableArray *)dataOne {

    if (!_dataOne) {
        
        _dataOne = [NSMutableArray array];
    }
    return _dataOne;
}

- (NSMutableArray *)dataSource {

    if (!_dataSource) {
    
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

- (MBProgressHUD *)hud {

    if (!_hud) {
        _hud = [[MBProgressHUD alloc] initWithView:self.view];
        _hud.labelText = @"正在加载";
    }
    return _hud;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
